import React, { Component } from 'react'
import { connect } from 'react-redux'
import { push } from 'connected-react-router'
import { dataset as datasetDuck } from 'ducks'

class DatasetNull extends Component {
  componentDidMount = () => {
    const { actionToDataset, datasets } = this.props
    if (datasets.result.length > 0) {
      const firstServer = datasets.entities.server[datasets.result[0]]
      if (firstServer.dataset_array.length > 0) {
        const firstDatasetName = firstServer.dataset_array[0]
        actionToDataset(firstDatasetName)
      }
    }
  }

  render() {
    return null
  }
}

const mapStateToProps = state => ({
  datasets: datasetDuck.selectors.datasets(state),
})

const mapDispatchToProps = {
  actionToDataset: path => push(`/dataset/${path}`),
}

export default connect(
  mapStateToProps,
  mapDispatchToProps,
)(DatasetNull)
